package com.wx.controller;

import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.processing.ProcessingEnvironment;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wx.model.BookType;
import com.wx.model.Books;
import com.wx.serviceImpl.BookServiceImpl;
import com.wx.util.pageUtil;

/**
 * Servlet implementation class BookServlet
 */
@WebServlet("/servlet/BookServlet")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String page = request.getParameter("page");
		
		BookServiceImpl bookServiceImpl = new BookServiceImpl();
	
		try {
			
			List<BookType> type_list = bookServiceImpl.queryAlltype();
			request.setAttribute("type_list", type_list);
			//便利图书类
			if(page.equals("1")){
				int typeid=Integer.parseInt( request.getParameter("typeid"));
				System.out.println(typeid);
				int pageNum = Integer.parseInt(request.getParameter("pageNum"));
				if(typeid==0){
					//查询总条数
					int bookSum = bookServiceImpl.queryBookSum();
					//计算页数		
					int pageCount=(int) Math.ceil(bookSum*1.0/pageUtil.PAGE_SIZE);
					List<Books> booklist = bookServiceImpl.querybook(pageNum,pageUtil.PAGE_SIZE);
					request.setAttribute("book_list", booklist);
					request.setAttribute("pageCount", pageCount);
				}else{
					//查询总条数
					int bookSum = bookServiceImpl.queryBookSumById(typeid);
					//计算页数		
					int pageCount=(int) Math.ceil(bookSum*1.0/pageUtil.PAGE_SIZE);
					List<Books> booklist = bookServiceImpl.queryBookById(pageNum, pageUtil.PAGE_SIZE, typeid);
					request.setAttribute("book_list", booklist);
					request.setAttribute("pageCount", pageCount);
				}
				request.setAttribute("value", typeid);
				request.setAttribute("pageNum", pageNum);
				request.getRequestDispatcher("../manage/book/book.jsp").forward(request, response);
			}else if(page.equals("2")){
				//将typy_list传入AddBook页
				request.getRequestDispatcher("../manage/book/AddBook.jsp").forward(request, response);
			}else if(page.equals("3")){
				//添加图书
				int typeid=Integer.parseInt( request.getParameter("typeid"));

				String bookname = request.getParameter("bookname");
				String author = request.getParameter("author");
				String press = request.getParameter("press");
				String pubtime = request.getParameter("pubtime");
				
				String dis = request.getParameter("dis");
				int bookcount=Integer.parseInt( request.getParameter("bookcount"));
				Double price=Double.parseDouble( request.getParameter("price"));
				String file = request.getParameter("file");
				System.out.println(file);
//				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//				Date date = (Date) df.parse(pubtime);
				java.sql.Date date=java.sql.Date.valueOf(pubtime);
				
				
				
				Books book = new Books(typeid, bookname, author, press, date, dis, bookcount, price,file);
				boolean b = bookServiceImpl.addBook(book);
				if(b){
					request.setAttribute("infrom", 1);
					request.getRequestDispatcher("../manage/book/AddBook.jsp").forward(request, response);
				}
			}
			
			//添加图书
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
